<?php

namespace App\Observers;

use App\Http\Controllers\RushBuy;
use App\Models\GrCoupon;
use App\Models\GrCouponActivity;
use App\Models\GrGood;
use App\Models\StoreCombination;
use Illuminate\Support\Facades\Log;

class StoreCombinationObserver
{
    /**
     * Handle the StoreCombination "created" event.
     *
     * @param  \App\Models\StoreCombination  $storeCombination
     * @return void
     */
    public function created(StoreCombination $storeCombination)
    {
        $storeCombinationId = $storeCombination->id;
        $activityStock = $storeCombination->stock;
        //取出优惠券库存   根据活动需求对库存进行减少
        $data = StoreCombination::where('id', $storeCombinationId)->pluck('product_id')->toArray();
        $product_id = $data[0];
        $productStock = GrGood::where('goods_id', $product_id)->pluck('stock_total')->toArray();
        $stock_total=$productStock[0]-$activityStock;
        GrGood::where('goods_id', $product_id)->update(['stock_total' => $stock_total]);
        $model = new RushBuy();
        $model->storeCombination($storeCombinationId, $stock_total);
    }

    /**
     * Handle the StoreCombination "updated" event.
     *
     * @param  \App\Models\StoreCombination  $storeCombination
     * @return void
     */
    public function updated(StoreCombination $storeCombination)
    {
        //
    }

    /**
     * Handle the StoreCombination "deleted" event.
     *
     * @param  \App\Models\StoreCombination  $storeCombination
     * @return void
     */
    public function deleted(StoreCombination $storeCombination)
    {
        //
    }

    /**
     * Handle the StoreCombination "restored" event.
     *
     * @param  \App\Models\StoreCombination  $storeCombination
     * @return void
     */
    public function restored(StoreCombination $storeCombination)
    {
        //
    }

    /**
     * Handle the StoreCombination "force deleted" event.
     *
     * @param  \App\Models\StoreCombination  $storeCombination
     * @return void
     */
    public function forceDeleted(StoreCombination $storeCombination)
    {
        //
    }
}
